package com.ruoyi.project.stop.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.redis.RedisCache;
import com.ruoyi.common.redis.RedisConst;
import com.ruoyi.project.stop.domain.WarnConfig;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.stop.domain.ShipRecord;
import com.ruoyi.project.stop.service.IShipRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 船舶信息录入Controller
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/stop/ship")
public class ShipRecordController extends BaseController
{
    private String prefix = "stop/ship";

    @Autowired
    private IShipRecordService shipRecordService;

    @Autowired
    private RedisCache redisCache;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    @ApiOperation("获取最新一条船舶信息")
    @GetMapping("/getLastOne")
    public AjaxResult getInfo()
    {
        ShipRecord shipRecord = shipRecordService.selectShipRecordByIdLast();
        if (shipRecord !=null)
        {
            return success(shipRecord);
        }
        else
        {
            return error("数据不存在");
        }
    }
    @GetMapping("/info")
    public AjaxResult info(@PathVariable("id") Long id)
    {
        ShipRecord shipRecord = shipRecordService.selectShipRecordById(id);

        return success(shipRecord);
    }
    /**
     * 查询船舶信息录入列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShipRecord shipRecord)
    {
        startPage();
        List<ShipRecord> list = shipRecordService.selectShipRecordList(shipRecord);
        return getDataTable(list);
    }

    /**
     * 导出船舶信息录入列表
     */
    @Log(title = "船舶信息录入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShipRecord shipRecord)
    {
        List<ShipRecord> list = shipRecordService.selectShipRecordList(shipRecord);
        ExcelUtil<ShipRecord> util = new ExcelUtil<ShipRecord>(ShipRecord.class);
        return util.exportExcel(list, "船舶信息录入数据");
    }

    /**
     * 新增船舶信息录入
     */

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存船舶信息录入
     */

    @Log(title = "船舶信息录入", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShipRecord shipRecord)
    {
        return toAjax(shipRecordService.insertShipRecord(shipRecord));
    }

    /**
     * 修改船舶信息录入
     */

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ShipRecord shipRecord = shipRecordService.selectShipRecordById(id);
        mmap.put("shipRecord", shipRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存船舶信息录入
     */

    @Log(title = "船舶信息录入", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShipRecord shipRecord)
    {
        return toAjax(shipRecordService.updateShipRecord(shipRecord));
    }

    /**
     * 删除船舶信息录入
     */

    @Log(title = "船舶信息录入", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shipRecordService.deleteShipRecordByIds(ids));
    }
}
